package com.das.mechanic_base.adapter.camera;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.camera.TagDetectionBean;

import java.util.ArrayList;
import java.util.List;

public class X3TagDetectionAdapter extends RecyclerView.Adapter<X3TagDetectionAdapter.TagDetectionHolder> {

    private List<TagDetectionBean> mList;

    private Context mContext;

    public X3TagDetectionAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeTagData(List<TagDetectionBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TagDetectionHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new TagDetectionHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_tag_detection_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull TagDetectionHolder holder, int position) {

        holder.tv_title.setText(((position + 1 < 10) ? "0" + (position + 1) : (position + 1)) + "." + mList.get(position).questionBaseDescription);

        List<TagDetectionBean.TagOptionBean> optionsList = mList.get(position).optionsDeductScoreVOList;

        X3TagDetectionItemAdapter itemAdapter = new X3TagDetectionItemAdapter(optionsList,mContext);

        holder.rlv_item.setAdapter(itemAdapter);

        itemAdapter.setiOnClickItem(content -> {
            if (iOnClickItem != null) {
                iOnClickItem.iOnClickItem(content);
            }
        });

    }

    IOnClickItem iOnClickItem;

    public X3TagDetectionAdapter setiOnClickItem(IOnClickItem iOnClickItem) {
        this.iOnClickItem = iOnClickItem;
        return this;
    }

    public interface IOnClickItem {
        void iOnClickItem(String content);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class TagDetectionHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        RecyclerView rlv_item;
        public TagDetectionHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            rlv_item = itemView.findViewById(R.id.rlv_item);

            ChipsLayoutManager userLayoutManager = ChipsLayoutManager.newBuilder(mContext)
                    .setChildGravity(Gravity.LEFT)
                    .setScrollingEnabled(true)
                    .setOrientation(ChipsLayoutManager.HORIZONTAL)
                    .build();
            rlv_item.setLayoutManager(userLayoutManager);
        }
    }
}
